<?php

/**
 * 
 * manage data of categories table
 *
 * @author tuancasi
 * 
 * properties of this table
 * @property int $category_id
 * @property string $image
 * @property int $name
 * @property int $time_created
 * @property int $descripttion	 
 * @property int $order_cat order of category	 
 */
class Category extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{categories}}';
    }

    public function rules() {
	return array(
	    array('name', 'required', 'on' => 'insert'),
	    array('order_cat', 'numerical'),
	    array('category_id, time_created, description, image', 'safe')
	);
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
	// NOTE: you may need to adjust the relation name and the related
	// class name for the relations automatically generated below.
	return array(
	);
    }

    /**
     * @param int $type is comment or post
     * @return \CActiveDataProvider
     */
    public function search() {
	// Warning: Please modify the following code to remove attributes that
	// should not be searched.

	$criteria = new CDbCriteria;
	$criteria->compare('name', $this->name, true);
	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => param('pageSize')
	    ),
	    'sort' => array(
		'defaultOrder' => array(
		    't.update_time' => CSort::SORT_DESC
		)
	    )
	));
    }

    public function afterSave() {
	parent::afterSave();
	//clear cache on front end
	_clearCache('aCat');
	//clear cache on backend
	$aCat = Yii::app()->cache->get('aCat');
	if ($aCat !== false) {
	    Yii::app()->cache->delete('aCat');
	}
    }

    public function afterDelete() {
	parent::afterSave();

	if ($model->image) {
	    deleteFile(UPLOAD_PATH . 'category' . DIRECTORY_SEPARATOR . $model->image);
	}

	//clear cache on front end
	_clearCache('aCat');
	//clear cache on backend
	$aCat = Yii::app()->cache->get('aCat');
	if ($aCat !== false) {
	    Yii::app()->cache->delete('aCat');
	}
    }

    public static function getListCat() {
//	Yii::app()->cache->delete('aCat');
	$aCat = Yii::app()->cache->get('aCat');
	if ($aCat === false) {
	    $aCat = array();
	    $cats = Yii::app()->db->createCommand('SELECT * FROM {{categories}}')->queryAll();
	    if ($cats) {
		$aCat = $cats;
	    }
	    Yii::app()->cache->set('aCat', $aCat);
	}
	return $aCat;
    }

    public function getImg($size = '') {
	switch ($size) {
	    case 'thumb' :
		$width = 200;
		$height = 150;
		break;
	    case 'small' :
		$width = 64;
		$height = 64;
		break;
	    case 'large':
		$width = 800;
		$height = 600;
		break;
	    default :
		return UPLOAD_URL . 'category/' . $this->image;
	}
	return ImageHelper::thumb($width, $height, 'category' . DIRECTORY_SEPARATOR . $this->image);
    }

}

